package com.example.controller;


import com.example.domain.po.Disease;
import com.example.domain.vo.DiseaseVo;
import com.example.service.DiseaseService;
import com.example.util.AliOSSUtils;
import com.example.util.PageBean;
import com.example.util.Result;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/disease")
public class DiseaseController {

    @Autowired
    private DiseaseService diseaseService;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @GetMapping("/list")
    public List<Disease> list(){
        return diseaseService.list();
    }

    @GetMapping("/listAll")
    public Result<List<Disease>> listAll(){
        return Result.success(diseaseService.listAll());
    }

    /**
     * 通过医生名字，病例名模糊查询病例信息
     * @return
     */
    @GetMapping("/listByName")
    public Result<List<Map<String,Object>>> listByName(String name){
        if(name==null)
            name="";
        return Result.success(diseaseService.listByName(name));
    }

    /**
     * 通过病例id查询病例详情
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public Result<List<DiseaseVo>> getById(Integer id){
        return Result.success(diseaseService.getById(id));
    }
  /*  @PostMapping("/addDisease")
    public Integer add( DiseaseDTO diseaseDTO){
        System.out.println(diseaseDTO);
        Integer id = diseaseService.addDisease(diseaseDTO);
        return id;
    }*/


/*
    @PostMapping("/addItem")
    public Result<String> add(String info, MultipartFile photoList, Integer id) throws IOException {
        StringBuffer sb = new StringBuffer();
       for (MultipartFile f : photoList) {
            sb.append(aliOSSUtils.upload(f)).append(",");
        }
        String s = sb.substring(0,sb.lastIndexOf(","));   //照片数组
        diseaseService.addItem(s,info ,id);
        return Result.success("添加成功");
    }
*/


    @PostMapping("/addItem2")
    public Result<String> add(String info, String[] photoList,Integer id) throws IOException {
        String string = Arrays.toString(photoList);//照片数组
        System.out.println(string);
        StringBuffer sb = new StringBuffer();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 解析 JSON 数据为 List<List<Map<String, Object>>> 类型
            List<List<Map<String, Object>>> data = objectMapper.readValue(string, new TypeReference<List<List<Map<String, Object>>>>() {});
            // 打印解析结果
            for (List<Map<String, Object>> list : data) {
                for (Map<String, Object> item : list) {
                    System.out.println("URL: " + item.get("url"));
                    sb.append(item.get("url")).append(",");
                    System.out.println("UID: " + item.get("uid"));
                    System.out.println("Status: " + item.get("status"));
                    System.out.println();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(sb);
        System.out.println(sb);
        System.out.println(sb);
        diseaseService.addItem(sb.toString(),info ,id);
        return Result.success("添加成功");
    }





    @GetMapping("page")
    public Result<PageBean<DiseaseVo>> list(
            Integer pageNum,
            Integer pageSize,
            String name,
            String category

    ) {
        return Result.success(diseaseService.list(pageNum, pageSize, name,category));
    }

   @GetMapping("/findById")
    public List<DiseaseVo> findById(Integer id){
        return diseaseService.findById(id);
    }




}
